<?php

class MainPage {

    public $LessonResult;
    //public $strOrderby="";
    public $orderby = "0";
    public $desc = "0";
    public $smarty = null;

    public function __construct() {
        include_once($_SERVER['DOCUMENT_ROOT'] . "/RecJP/class/DAL/DAL.danciOperator.php");
        include_once($_SERVER['DOCUMENT_ROOT'] . "/RecJP/class/DAL/DAL.LessonOperator.php");
        include_once($_SERVER['DOCUMENT_ROOT'] . "/RecJP/inc/smarty_inc.php");
        include_once($_SERVER['DOCUMENT_ROOT'] . "/RecJP/class/DAL/DAL.userMemory.php");
        include_once($_SERVER['DOCUMENT_ROOT'] . "/RecJP/class/Model/BModel.Danci.php");
        $this->smarty = getSmarty();
        //$this->s=  getSqliteHelper();
        //$sql="SELECT distinct lesson FROM danci";
        $LessonResult = Dal_lessonOperator::GetLessonList();
        //$xinbian1 = array();
        //$dajia = array();
        foreach ($LessonResult as $key => $value) {
            if ($value["bookid"] == 1) {
                $xinbian1[] = $value["lesson"];
            } else if ($value["bookid"] == 2) {
                $xinbian2[] = $value["lesson"];
            } else if ($value["bookid"] == 3) {
                $dajia[] = $value["lesson"];
            }
        }
        $this->smarty->assign("xinbian1", $xinbian1);
        $this->smarty->assign("xinbian2", $xinbian2);
        $this->smarty->assign("dajia", $dajia);
    }

    public function ShowRandom($Code_orderby) {
        $arrayid = Dal_danciOperator::GetRandomWordid(20);
        $sql = "SELECT * FROM danci where wordid in (-1";
        foreach ($arrayid as $value) {
            $sql.="," . $value;
        }
        $sql.=")";
        $BModel_danci = new BModel_danci();
        $BModel_danci->userid = $_SESSION["userid"];
        $result = Dal_danciOperator::selectRandom($BModel_danci, $arrayid, $this->GetOrderby($Code_orderby), $this->GetOrder($Code_orderby));
        $this->display($this->orderby, $this->desc, $result);
    }

    public function Show($bookid, $lesson, $Code_orderby) {
        $BModel_danci = new BModel_danci();
        $BModel_danci->userid = $_SESSION["userid"];
        $BModel_danci->lesson = $lesson;
        $BModel_danci->bookid = $bookid;
        $result = Dal_danciOperator::select($BModel_danci, $this->GetOrderby($Code_orderby), $this->GetOrder($Code_orderby));
        $BookName = Dal_lessonOperator::GetBookName($bookid);
        $this->smarty->assign("BookName", $BookName);
        $this->smarty->assign("bookid", $bookid);
        $this->display($this->orderby, $this->desc, $result);
    }

    private function GetOrder($Code_orderby) {
        $strOrder = "";
        if (empty($Code_orderby) == false) {
            $arrayordeyby = explode(",", $Code_orderby);
            $this->desc = $arrayordeyby[1];
            if ($arrayordeyby[1] == "0") {
                $strOrder.=" asc";
            } else if ($arrayordeyby[1] == "1") {
                $strOrder.=" desc";
            }
        } else {
            $strOrder = "asc";
        }

        return $strOrder;
    }

    private function GetOrderby($Code_orderby) {
        $strOrderby = "";
        if (empty($Code_orderby) == false) {
            $arrayordeyby = explode(",", $Code_orderby);
            $this->orderby = $arrayordeyby[0];
            if ($arrayordeyby[0] == "0") {
                $strOrderby = " order by id ";
            } else if ($arrayordeyby[0] == "1") {
                $strOrderby = " order by word ";
            } else if ($arrayordeyby[0] == "2") {
                $strOrderby = " order by read ";
            } else if ($arrayordeyby[0] == "3") {
                $strOrderby = " order by meaning ";
            } else if ($arrayordeyby[0] == "4") {
                $strOrderby = " order by errorrate ";
            }
        } else {
            $strOrderby = " order by id";
        }
        return $strOrderby;
    }

    private function display($orderby, $desc, $result) {
        $this->smarty->assign("orderby", $orderby);
        $this->smarty->assign("desc", $desc);
        $this->smarty->assign("Words", $result);
        $this->smarty->display('Main.tpl.htm');
    }

//        public function InitQuickTest(){
//            include_once($_SERVER['DOCUMENT_ROOT']."/RecJP/class/class.QuickTest.php");
//            $QuickTest = new QuickTest();
//            $correctItem = null;//干扰项
//            $wid=1;
//            $Options = $QuickTest->GetOptions($wid, $correctItem);
//            $this->smarty->assign("lessonid", 5);
//            $this->smarty->assign("correctItem", $correctItem);
//            $this->smarty->assign("Options", $Options);
//        }
}
